//
//  YDUserModel.swift
//  weibo
//
//  Created by 孙亚东 on 16/6/24.
//  Copyright © 2016年 Sunyadong. All rights reserved.
//

import UIKit

class YDUserModel: NSObject, NSCoding {
    
    /**
    *
    "access_token" = "2.00TB2V3GBuav5Bbc6155a1f6h85dhD";
    "expires_in" = 157679999;
    "remind_in" = 157679999;
    uid = 5829345367;
    
    */
    
    var access_token: String?
    
    
    //重写set方法，给expires_data 赋值
    var expires_in: NSTimeInterval  = 0 {

        didSet {
        
        expires_data = NSDate(timeIntervalSinceNow: expires_in)
        
        }
    
    }
    
    
    var expires_data: NSDate?
    
//    var remind_in: String?
    
    var uid: String?

    
    //同一个模型中添加两个属性
    
    var screen_name: String?
    
    var profile_image_url: String?
    
    
    
    //重写kvc构造方法
    init(dict: [String: AnyObject]) {
        
        // 先调用父类的初始化方法
        super.init()

        
        setValuesForKeysWithDictionary(dict)
        
    }
    
    //实现下面的方法，防止崩溃，字典中存在模型中没有定义的属性
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
        
        
        
    }
    
    
    //进行归档解档
    
    
    //解档
    required init?(coder aDecoder: NSCoder) {
        

        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = (aDecoder.decodeObjectForKey("expires_in") as? NSTimeInterval)!
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        profile_image_url = aDecoder.decodeObjectForKey("profile_image_url") as? String
        
        
    }
    
    
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(profile_image_url, forKey: "profile_image_url")

        
        
    }
    
    
    //定义归档方法
    
    func saveModel () {
    
        //获取沙河路径
        
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last)!
        
        
        //拼接路径
        
       let filePath = (path as NSString).stringByAppendingPathComponent("aaa.aaa")
        
        NSKeyedArchiver.archiveRootObject(self, toFile: filePath)
        

    }
    
    
    //解档
    class func readModel ()-> YDUserModel?{
        
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last)!
        
        
        //拼接路径
        
        let filePath = (path as NSString).stringByAppendingPathComponent("aaa.aaa")
        
        let model = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? YDUserModel
        
        if let date = model?.expires_data {
        
            if   date.compare(NSDate()) == NSComparisonResult.OrderedDescending{
            
                return model
            
            
            }else{
            
            
                return nil
            
                }
 
        }
        
      return model
        
        
        
    }
    
    
    
    
    
    
    
    

}
